﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Text;

namespace Directory.Web
{
	public partial class Controls_CascadingCategory : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
		}

		public override void DataBind()
		{
			base.DataBind();
			if (DataSource != null && DataSource.Count > 0)
			{
				StringBuilder sb = new StringBuilder();
				sb.Append("<select id='_select_0' name='_select_0' onchange='SelectChange(this,null)'>");
				if (this.SelectedIndex == 0)
				{
					sb.Append("<option value='-999' selected='selected'>选择</option>");
				}
				else
				{
					sb.Append("<option value='-999'>选择</option>");
				}
				for (int i = 0; i < DataSource.Count; i++)
				{
					if (i == this.SelectedIndex-1)
					{
						sb.Append(string.Format("<option value='{0}' selected='selected'>{1}</option>",DataSource[i].CategoryId, DataSource[i].CategoryName));
						tbSelectedId.Value = DataSource[i].CategoryId.ToString();
					}
					else
					{
						sb.Append(string.Format("<option value='{0}'>{1}</option>", DataSource[i].CategoryId, DataSource[i].CategoryName));
					}
				}
				sb.Append("</select>");
				ltInit.Text = sb.ToString();
			}
		}

		public IList<Directory.Model.CategoryInfo> DataSource;

		private string _responsetUrl = ConfigurationManager.AppSettings["AjaxResponsepage"];
		public string ResponseUrl
		{
			get { return _responsetUrl; }
			set { _responsetUrl = value; }
		}

		public int SelectedId
		{
			get {
				int Id;
				if (!int.TryParse(tbSelectedId.Value, out Id))
				{
					Id=-999;
				}
				return Id;
			}
		}

		private int _selectedIndex;
		public int SelectedIndex
		{
			get { return _selectedIndex; }
			set { _selectedIndex = value; }
		}

		private bool _autoLoad = false;
		public bool AutoLoad
		{
			get { return _autoLoad; }
			set { _autoLoad = value; }
		}

		private string _selectedIndexStr;
		public string SelectedIndexStr
		{
			get { return _selectedIndexStr; }
			set { _selectedIndexStr = value; }
		}
	}
}